package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.query.AccidentQueryObject;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.service.IStoragesService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("accident")
public class AccidentController {
    @Autowired
    private IAccidentService service;
    @Autowired
    private IStoragesService storagesService;
    @RequiresPermissions("accident:index")
    @PermissionName("事故备案首页")

    @RequestMapping("")
    private String index(){
        return "accident/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("accident:list")
    @PermissionName("待验证事故备案列表")
    private PageResult list(AccidentQueryObject qo){
        return service.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("accident:save")
    @PermissionName("事故备案数据保存")
    private JsonResult save(Accident accident){
        JsonResult result = new JsonResult();
        try {
            service.save(accident);
            //更改storages中的accident_state状态为1
            storagesService.changeAccidenState(accident.getInsuranceNo());

        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存失败");
        }
        return result;
    }
    /*
    * 删除数据失败
    * */
    @RequestMapping("deleteAccident")
    @ResponseBody
    @RequiresPermissions("accident:deleteAccident")
    @PermissionName("事故备案数据删除")
    private JsonResult deleteAccident(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("删除数据失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("accident:update")
    @PermissionName("事故备案数据更新")
    private JsonResult update(Accident accident){
        JsonResult result = new JsonResult();
        try {
            service.update(accident);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("编辑失败");
        }
        return result;
    }

    @RequestMapping("allList")
    @ResponseBody
    @RequiresPermissions("accident:allList")
    @PermissionName("所有事故备案数据信息")
    private PageResult allList(AccidentQueryObject qo){
        return  service.allList(qo);
    }

    @RequestMapping("viewAccident")
    @ResponseBody
    private Accident viewAccident(Long id){
        return service.get(id);
    }
}
